package com.taoist_awei.blog.infrastructure.service;

import com.taoist_awei.blog.core.UserLoginService;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class DefaultUserLoginService implements UserLoginService {
    private Map<String, String> loginMap;

    public DefaultUserLoginService() {
        loginMap = new HashMap<>();
    }

    @Override
    public boolean isLogin(String userId) {
        return loginMap.containsKey(userId);
    }

    @Override
    public void login(String userId, String token) {
        synchronized (this) {
            loginMap.put(userId, token);
        }
    }

    @Override
    public void logout(String userId) {
        synchronized (this) {
            loginMap.remove(userId);
        }
    }

    @Override
    public String getToken(String userId) {
        return loginMap.get(userId);
    }

    @Override
    public Map<String, String> getLoginMap() {
        synchronized (this) {
            Map<String, String> newMap = new HashMap<>(loginMap);
            return newMap;
        }
    }
}
